#pragma once
#include <QQuickView>
#include <osgGA/EventQueue>

class OSGQuickView : public QQuickView {
public:
	OSGQuickView(const QString& qmlSource, osgGA::EventQueue* eventQueue = nullptr): m_eventQueue(eventQueue) {
		setSurfaceType(QQuickView::SurfaceType::OpenGLSurface);
		QSurfaceFormat format;
		format.setProfile(QSurfaceFormat::CoreProfile);
		format.setVersion(4, 6);
		format.setSamples(4);
		setFormat(format);

		setSource(QUrl(qmlSource));// e.g "../test/quick/main.qml"
	}

protected:
	void mousePressEvent(QMouseEvent* event) override {
		if (!m_eventQueue) return;
		m_eventQueue->mouseButtonPress(event->x(), event->y(), convertQtMouseButton(event->button()));
	}

	void mouseReleaseEvent(QMouseEvent* event) override {
		if (!m_eventQueue) return;
		m_eventQueue->mouseButtonRelease(event->x(), event->y(), convertQtMouseButton(event->button()));
	}

	void mouseMoveEvent(QMouseEvent* event) override {
		if (!m_eventQueue) return;
		m_eventQueue->mouseMotion(event->x(), event->y());
	}

	void keyPressEvent(QKeyEvent* event) override {
		if (!m_eventQueue) return;
		m_eventQueue->keyPress(osgGA::GUIEventAdapter::KeySymbol(event->key()));
	}

	void keyReleaseEvent(QKeyEvent* event) override {
		if (!m_eventQueue) return;
		m_eventQueue->keyRelease(osgGA::GUIEventAdapter::KeySymbol(event->key()));
	}

private:
	int convertQtMouseButton(Qt::MouseButton button) {
		switch (button) {
		case Qt::LeftButton: return 1;
		case Qt::MiddleButton: return 2;
		case Qt::RightButton: return 3;
		default: return 0;
		}
	}

	osgGA::EventQueue* m_eventQueue;
};